﻿using System;
using System.ComponentModel.DataAnnotations;

namespace TK.Common.DataAnnotations
{
    /// <summary>
    /// Specifies the property must earlier than current time.
    /// </summary>
    public class EarlierThanCurrentTimeAttribute : ValidationAttribute
    {
        /// <summary>
        /// Determines whether the specified value of the object is valid.
        /// </summary>
        public override bool IsValid(object value)
        {
            if (value != null)
            {
                if (DateTimeOffset.TryParse(value.ToString(), out var curVal))
                {
                    return curVal <= DateTimeOffset.Now;
                }
            }

            return true;
        }
    }
}
